/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[Exceptions.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package jdk7;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * jdk 1.7 异常新特性
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class Exceptions {

	private static String username;
	private static String password;
	private static String url;
	private static String driverClass;

	static {
		Properties prop = new Properties();
		try {
			prop.load(Exceptions.class.getClassLoader().getResourceAsStream("jdk7/dbconfig.properties"));
			username = prop.getProperty("username");
			password = prop.getProperty("password");
			url = prop.getProperty("url");
			driverClass = prop.getProperty("driverClass");

			Class.forName(driverClass);
		} catch (IOException | ClassNotFoundException e) {
			// 一个catch语句处理多个异常, 多个异常之间使用 | 分割, 多个异常类之间不能存在继承关系
			e.printStackTrace();
		}
	}

	/**
	 * try-with-resources<br/>
	 * 任何实现了java.lang.AutoCloseable接口的类都可以使用try-with-resources结构<br/>
	 * 任何在try括号内声明的资源会在一个隐式的finally语句块中关闭<br/>
	 * 
	 * 例如, JDBC的Connection, PreparedStatement和ResultSet都继承了该接口<br/>
	 */
	public static void feature() {
		String sql = "SELECT * FROM User";
		try (Connection connection = DriverManager.getConnection(username, password, url);
				PreparedStatement statement = connection.prepareStatement(sql)) {
			try (ResultSet resultSet = statement.executeQuery()) {
				// iterator resultSet
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
